package com.wongoing.util;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

/**
 * 功能说明：日志记录服务类
 * 修改说明：
 * @author zhenglibing
 * @date 2017-4-25 下午4:01:24
 * @version V0.1
 */
public final class LogService {
	private static LogService instance;
	private static Logger logger = null;
	private static Map<Class, Logger> loggerList = new HashMap<Class, Logger>();	//用于缓存logger对象
	
	/**
	 * 定义私有构造方法实现单例
	 */
	private LogService() {
	}
	
	/**
	 * 功能说明：获取服务实例的静态方法
	 * 修改说明：
	 * @author zhenglibing
	 * @date 2017-4-25 下午4:02:09
	 * @param obj 传入调用此方法的对象
	 * @return
	 */
	public synchronized static LogService getInstance(Object obj) {
		if (instance == null) {
			instance = new LogService();
		}
		LogService.logger = loggerList.get(obj.getClass());
		if (LogService.logger == null) {
			LogService.logger = Logger.getLogger(obj.getClass());
			loggerList.put(obj.getClass(), LogService.logger);
		}
		return instance;
	}
	
	/**
	 * 功能说明：获取服务实例的静态方法
	 * 修改说明：
	 * @author zhenglibing
	 * @date 2017-4-25 下午4:03:10
	 * @param clazz 传入调用此方法的类型
	 * @return
	 */
	public synchronized static LogService getInstance(Class clazz) {
		if (instance == null) {
			instance = new LogService();
		}
		LogService.logger = loggerList.get(clazz);
		if (LogService.logger == null) {
			LogService.logger = Logger.getLogger(clazz);
			loggerList.put(clazz, LogService.logger);
		}
		return instance;
	}
	
	/**
	 * 功能说明：获取服务实例的静态方法
	 * 修改说明：
	 * @author zhenglibing
	 * @date 2017-4-25 下午4:05:13
	 * @return
	 */
	public synchronized static LogService getInstance() {
		if (instance == null) {
			instance = new LogService();
		}
		LogService.logger = loggerList.get(LogService.class);
		if (LogService.logger == null) {
			LogService.logger = Logger.getLogger(LogService.class);
			loggerList.put(LogService.class, LogService.logger);
		}
		return instance;
	}
	
	public void trace(Object message) {
		LogService.logger.trace(message);
	}
	
	public void trace(Object message, Throwable t) {
		LogService.logger.trace(message, t);
	}
	
	public void debug(Object message) {
		LogService.logger.debug(message);
	}
	
	public void debug(Object message, Throwable t) {
		LogService.logger.debug(message, t);
	}
	
	public void info(Object message) {
		LogService.logger.info(message);
	}
	
	public void info(Object message, Throwable t) {
		LogService.logger.info(message, t);
	}
	
	public void warn(Object message) {
		LogService.logger.warn(message);
	}
	
	public void warn(Object message, Throwable t) {
		LogService.logger.warn(message, t);
	}
	
	public void error(Object message) {
		LogService.logger.error(message);
	}

	public void error(Object message, Throwable t) {
		LogService.logger.error(message, t);
	}
}
